package com.googlecode.androidspamblocker;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsRuleManager {
	private static final String TAG = "SmsRuleManager";
	private static final String PERSONAL_RULE_File = ".personal";
	private Context context;
	private SmsRule personalRule;
	private SmsRule contactRule;
	private SmsRule publicRule;
	
	public SmsRuleManager(Context context) {
		this.context = context;
		personalRule = new SmsCustomFilters();
		contactRule = new SmsContactRule();
		publicRule = new SmsCustomFilters();
	}
	
	public void init() {
		Log.d(TAG, "init");
		initPersonalRule();
	}
	
	public void persist() {
		Log.d(TAG, "persist");
		persistPersonalRule();
	}
	
	public SmsRule getPersonalRule() {
		return personalRule;
	}
	
	public int getAction(SmsMessage msg) {
		int action = SmsAction.UNKNOWN;
		action = personalRule.match(msg, context);
		if(action != SmsAction.UNKNOWN) {
			return action;
		}
		action = contactRule.match(msg, context);
		if(action != SmsAction.UNKNOWN) {
			return action;
		}
		action = publicRule.match(msg, context);
		if(action != SmsAction.UNKNOWN) {
			return action;
		}
		return action;
	}
	
	public int getAction(SmsMessage[] msg) {
		int action = SmsAction.UNKNOWN;
		action = personalRule.match(msg, context);
		if(action != SmsAction.UNKNOWN) {
			return action;
		}
		action = contactRule.match(msg, context);
		if(action != SmsAction.UNKNOWN) {
			return action;
		}
		action = publicRule.match(msg, context);
		if(action != SmsAction.UNKNOWN) {
			return action;
		}
		return action;
	}
	
	public boolean addFilter(Bundle bundle) {
		Log.d(TAG, "addFilter");
		String type = bundle.getString("SmsFilter");
		if(type != null) {
			try {
				SmsFilter filter;
				filter = (SmsFilter) Class.forName(type).newInstance();
				filter.load(bundle);
				return ((SmsCustomFilters) personalRule).add(filter);
			} catch (IllegalAccessException e) {
				Log.w(TAG, e);
			} catch (InstantiationException e) {
				Log.w(TAG, e);
			} catch (ClassNotFoundException e) {
				Log.w(TAG, e);
			}
		}
		return false;
	}
	
	public boolean addFilter(SmsFilter filter) {
		Log.d(TAG, "addFilter");
		return ((SmsCustomFilters)personalRule).add(filter);
	}
	
	private void initPersonalRule() {
		Log.d(TAG, "initPersonalRule");
		try {
			FileInputStream file = context.openFileInput(PERSONAL_RULE_File);
			byte[] buf = new byte[(int) file.getChannel().size()];
			file.read(buf);
			String str = new String(buf);
			personalRule.load(str);
			file.close();
		} catch(IOException e) {
			Log.w(TAG, "Failed to initialize personal filter rules from file " + e);
		}
		
	}
	
	private void persistPersonalRule() {
		Log.d(TAG, "persistPersonalRule");
		try {
			FileOutputStream file = context.openFileOutput(PERSONAL_RULE_File, Context.MODE_PRIVATE);
			file.write(personalRule.toString().getBytes());
			file.close();
		} catch(IOException e) {
			Log.w(TAG, "Failed to store personal filter rules to internal storage " + e);
		}
	}

}
